#version 150

const float globalColorFactor = 0.75;

// material
uniform vec3 materialKd;
uniform vec3 materialKs;
uniform float materialNs;

uniform vec4 isMultiText;
// texture
uniform sampler2D diffuseTexture;
uniform sampler2D specularTexture;
uniform sampler2D shininessTexture;
uniform sampler2D mapTexture;

//uniform sampler2D baseTexture;
uniform sampler2DRect textNormDepth;

// fragment
in vec3 varNormal;
in vec4 varTexCoord;

// lumieres
in vec3 lightDirInView[3];
in vec3 halfVecInView[3];

// material
uniform vec3 keyLightColor;
uniform vec3 fillLightColor;
uniform vec3 backLightColor;

// resultat
out vec4 outColor;

// --------------------

vec3 blinnPhongLighting (in vec3 kd, in vec3 ks, in float ns, in vec3 normal, in vec3 lightVector, in vec3 halfVector){
    // Fonction à écrire par les étudiants

    halfVector = normalize(halfVector + lightVector);

    float costetha = clamp(dot(normal, lightVector), 0., 1.);

    float cosalphan = pow( clamp( dot(halfVector,normal), 0., 1.), ns );

    vec3 result = (kd + (ks* cosalphan)) * costetha;
    return result;
}

void main(void) {

    vec3 fragColor = vec3(0.0, 0.0, 0.0);
//    fragColor = blinnPhongLighting(materialKd, materialKs, materialNs, normalize(varNormal), normalize(lightDirInView[0]), normalize(halfVecInView[0]));
    vec3 kd;
    if(isMultiText.x == 1.){
      kd = texture(diffuseTexture, varTexCoord.st).rgb;
     }
     else{
     kd = materialKd;
      }
    vec3 ks;
    if(isMultiText.y == 1.){
      // ks = texture(specularTexture, varTexCoord.st).rgb;
      }
      else{
        ks = materialKs;
       }
    float ns;
    if(isMultiText.z == 1.){
  //   ns = texture( shininessTexture, varTexCoord.st).r;
    }
    else{
     ns = materialNs;
    }



    fragColor = fragColor + keyLightColor*blinnPhongLighting(kd, ks, ns, normalize(varNormal), normalize(lightDirInView[0]), normalize(halfVecInView[0]));
    fragColor = fragColor + fillLightColor*blinnPhongLighting(kd, ks, ns, normalize(varNormal), normalize(lightDirInView[1]), normalize(halfVecInView[1]));
    fragColor = fragColor + backLightColor*blinnPhongLighting(kd, ks, ns, normalize(varNormal), normalize(lightDirInView[2]), normalize(halfVecInView[2]));




   outColor = vec4( fragColor*globalColorFactor, 1.);
   // outColor = texture(textNormDepth, gl_FragCoord.st);
}

